/* Warranty
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
 * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
 * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * WE ARE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
 * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
 * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
 * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
 * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
 * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
 *
 **************************************************************************/
 
 
/****************************************************************************
  SECTION 	Include
****************************************************************************/
#include "TCPIPConfig.h"

#if defined(STACK_USE_HTTP2_SERVER)

#include "TCPIP Stack/TCPIP.h"



/****************************************************************************
  SECTION	Define
****************************************************************************/
#define __HTTPAPP_C

extern const int maxCol;
extern float redVal[];
extern float greenVal[];
extern float blueVal[];

/****************************************************************************
  SECTION 	Authorization Handlers
****************************************************************************/
 

/*****************************************************************************
  FUNCTION	BYTE HTTPNeedsAuth(BYTE* cFile)

  This function is used by the stack to decide if a page is access protected.
  If the function returns 0x00, the page is protected, if returns 0x80, no 
  authentication is required
*****************************************************************************/
#if defined(HTTP_USE_AUTHENTICATION)
BYTE HTTPNeedsAuth(BYTE* cFile)
{
	//	If you want to restrict the access to some page, include it in the folder "protect"
	//	here you can change the folder, or add others
	if(memcmppgm2ram(cFile, (ROM void*)"protect", 7) == 0)
		return 0x00;		// Authentication will be needed later


	// You can match additional strings here to password protect other files.
	// You could switch this and exclude files from authentication.
	// You could also always return 0x00 to require auth for all files.
	// You can return different values (0x00 to 0x79) to track "realms" for below.

	return 0x80;			// No authentication required
}
#endif

/*****************************************************************************
  FUNCTION	BYTE HTTPCheckAuth(BYTE* cUser, BYTE* cPass)
	
  This function checks if username and password inserted are acceptable

  ***************************************************************************/
#if defined(HTTP_USE_AUTHENTICATION)
BYTE HTTPCheckAuth(BYTE* cUser, BYTE* cPass)
{
	if(strcmppgm2ram((char *)cUser,(ROM char *)"admin") == 0
		&& strcmppgm2ram((char *)cPass, (ROM char *)"flyport") == 0)
		return 0x80;		// We accept this combination
	
	// You can add additional user/pass combos here.
	// If you return specific "realm" values above, you can base this 
	//   decision on what specific file or folder is being accessed.
	// You could return different values (0x80 to 0xff) to indicate 
	//   various users or groups, and base future processing decisions
	//   in HTTPExecuteGet/Post or HTTPPrint callbacks on this value.
	
	return 0x00;			// Provided user/pass is invalid
}
#endif

/****************************************************************************
  SECTION	GET Form Handlers
****************************************************************************/
  
/****************************************************************************
  FUNCTION	HTTP_IO_RESULT HTTPExecuteGet(void)
	
  This function processes every GET request from the pages. In the example, 
  it processes only the led.cgi function, but you can add code to process 
  other GET requests.
*****************************************************************************/
HTTP_IO_RESULT HTTPExecuteGet(void)
{
	BYTE *ptr;
	BYTE filename[20];
	BYTE *red;
	BYTE *green;
	BYTE *blue;
	
	// Load the file name
	// Make sure BYTE filename[] above is large enough for your longest name
	MPFSGetFilename(curHTTP.file, filename, 20);

	// If it's the LED updater file
	if(!memcmppgm2ram(filename, "leds.cgi", 8))
	{
		// Determine which LED to toggle
		ptr = HTTPGetROMArg(curHTTP.data, (ROM BYTE *)"led");
		// Toggle the specified LED
		switch(*ptr) 
		{
		
			case '0':
				IOPut(o1,toggle);
				break;
			case '1':
				IOPut(o2,toggle);
				break;
			case '2':
				IOPut(o3,toggle);
				break;
			case '3':
				IOPut(o4,toggle);
				break;
			case '4':
				IOPut(o5,toggle);
				break;
		}
		
	}


	// If it's the RGB controller file
	if(!memcmppgm2ram(filename, "rgb.cgi", 8))
	{
		int number = 1;
		BYTE *num;
		// Determine the values of each PWM to change
		num = HTTPGetArg(curHTTP.data, (BYTE *)"number"); 
		red = HTTPGetArg(curHTTP.data, (BYTE *)"red");
		green = HTTPGetArg(curHTTP.data, (BYTE *)"green");
		blue = HTTPGetArg(curHTTP.data, (BYTE *)"blue");
		char values[100];
		/*
		sprintf(values, "R:%s, G:%s, B:%s\r\n", red, green, blue);
		UARTWrite(1, values);
		*/
		number = atoi((char*)num);

		if((number > 0) && (number < 4))
		{
			redVal[number-1] = atof((char*)red);
			greenVal[number-1] = atof((char*)green);
			blueVal[number-1] = atof((char*)blue);
			
			sprintf(values, "Number: %d, R:%3.1f, G:%3.1f, B:%3.1f\r\n", 
							number, (double)redVal[number-1], 
							(double)greenVal[number-1], (double)blueVal[number-1]);
			UARTWrite(1, values);
			
		}
		else
		{
			UARTWrite(1, "Unable to give color number...\r\n");
		}
	}	
	
	return HTTP_IO_DONE;
}



/***************************************************************************
  SECTION	Dynamic Variable Callback Functions
****************************************************************************/

/****************************************************************************
  FUNCTION	void HTTPPrint_varname(void)
	
  Internal: See documentation in the TCP/IP Stack API or HTTP2.h for details.
 ***************************************************************************/

ROM BYTE HTML_UP_ARROW[] = "up";
ROM BYTE HTML_DOWN_ARROW[] = "dn";

void HTTPPrint_pot(WORD num)
{
	BYTE AN0String[8];
	WORD ADval;

	switch(num)
	{
		case 0:
			ADval = ADCVal(1);//(WORD)ADC1BUF0;
			uitoa(ADval, (BYTE*)AN0String);
			break;
		case 1:
			ADval = ADCVal(2);//(WORD)ADC1BUF1;
			uitoa(ADval, (BYTE*)AN0String);
			break;	
	}

   	TCPPutString(sktHTTP, AN0String);
}

void HTTPPrint_btn(WORD num)
{
	// Determine which button
	switch(num)
	{
		case 0:
			num = IOGet(i1);
			break;
		case 1:
			num = IOGet(i2);
			break;
		case 2:
			num = IOGet(i3);
			break;
		case 3:
			num = IOGet(i4);
			break;
		case 4:
			num = IOGet(i5);
			break;
		default:
			num = 0;
	}

	// Print the output
	TCPPutROMString(sktHTTP, (num?HTML_UP_ARROW:HTML_DOWN_ARROW));
	return;
}
	
void HTTPPrint_led(WORD num)
{
	// Determine which LED
	switch(num)
	{
		case 0:
			num = IOGet(o1);
			break;
		case 1:
			num = IOGet(o2);
			break;
		case 2:
			num = IOGet(o3);
			break;
		case 3:
			num = IOGet(o4);
			break;
		case 4:
			num = IOGet(o5);
			break;
		default:
			num = 0;
	}

	// Print the output
	TCPPut(sktHTTP, (num?'1':'0'));
	return;
}


#ifdef HTTP_USE_POST
/****************************************************************************
  FUNCTION	HTTP_IO_RESULT HTTPExecutePost(void)
	
  This function processes every POST request from the pages. 
*****************************************************************************/
HTTP_IO_RESULT HTTPExecutePost(void)
{

	
	return HTTP_IO_DONE;
}
#endif


#endif
